package com.atguigu.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.service.impl.AttrServiceImpl;
import com.atguigu.gulimall.product.vo.AttrGroupRelationVo;
import com.atguigu.gulimall.product.vo.WithAttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;

import javax.annotation.Resource;


/**
 * 属性分组
 *
 * @author YuGuoJian
 * @email yu925074596@163.com
 * @date 2021-11-22 22:27:49
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Resource
    private CategoryService categoryService;
    @Resource
    private AttrServiceImpl attrService;

    @Resource
    private AttrAttrgroupRelationService attrAttrgroupRelationService;


    /**
     * 列表
     * @param params:分页参数
     * @param id: catelog_id
     */
    @RequestMapping("/list/{id}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable Long id){
        PageUtils page = attrGroupService.queryPage(params,id);

        return R.ok().put("page", page);
    }

    @GetMapping("{attrgroupId}/attr/relation")
    public R AttrRelation(@PathVariable("attrgroupId") Long attrgroupId){
       List<AttrEntity> data = attrService.getAttrRelation(attrgroupId);
        return R.ok().put("data",data);
    }

    //product/attrgroup/attr/relation/delete
    @PostMapping("/attr/relation/delete")
    public R relationDelete(@RequestBody List<AttrGroupRelationVo> attrGroupRelationVo){
        attrGroupService.relationDelete(attrGroupRelationVo);
        return R.ok();
    }

    @PostMapping("/attr/relation")
    public R relation(@RequestBody List<AttrGroupRelationVo> attrGroupRelationVo){
        attrAttrgroupRelationService.addRelation(attrGroupRelationVo);
        return R.ok();
    }
    //  product/attrgroup/{catelogId}/withattr  获取分类下所有分组&关联属性
    @GetMapping("/{catelogId}/withattr")
    public R withAttr(@PathVariable("catelogId") Long catelogId){
        List<WithAttrVo> vos = attrGroupService.withAttr(catelogId);
        return R.ok().put("data",vos);
    }

    //product/attrgroup/2/noattr/relation
    @GetMapping("/{attrGroupId}/noattr/relation")
    public R noAttrRelation(@PathVariable("attrGroupId") Long attrGroupId,@RequestParam Map<String, Object> params){
        PageUtils page = attrService.noAttrRelation(params,attrGroupId);
        return R.ok().put("page",page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        attrGroup.setCatelogPath(categoryService.findCatelogPath(attrGroup.getCatelogId()));
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
